const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const passport = require('passport');
const app = express();

// 引入users.js
const users = require('./routes/api/users');
// 引入maps.js
const maps = require('./routes/api/maps');
// 引入gyms.js
const gyms = require('./routes/api/gyms');
// 引入goods.js
const goods = require('./routes/api/goods');
// 引入equipments.js
const equipments = require('./routes/api/equipments');
// 引入skills.js
const skills = require('./routes/api/skills');
// 引入shops.js
const shops = require('./routes/api/shops');
// 引入shops.js
const npcs = require('./routes/api/npcs');
// 引入schools.js
const schools = require('./routes/api/schools');
// 引入shops.js
const battles = require('./routes/api/battles');
// 引入open_func.js
const open_func = require('./controller/open_func');

// DB config
const db = require('./config/keys').mongoURI;

// 使用body-parser中间件
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// Connect to mongodb
mongoose.set('useCreateIndex', true)
mongoose
  .connect(
    db,
    { useNewUrlParser: true }
  )
  .then(() => console.log('MongoDB Connected'))
  .catch(err => console.log(err));

// passport 初始化
app.use(passport.initialize());

require('./config/passport')(passport);



// 执行前端静态页面
// app.use(express.static('client/dist'));
// app.get('*',(req, res) => {
//   res.sendFile(path.resolve(__dirname, 'client', 'dist', 'index.html'));
// });

// 使用routes
app.use('/api/users', users);
app.use('/api/maps', maps);
app.use('/api/gyms', gyms);
app.use('/api/goods', goods);
app.use('/api/equipments', equipments);
app.use('/api/skills', skills);
app.use('/api/shops', shops);
app.use('/api/npcs', npcs);
app.use('/api/battles', battles);
app.use('/api/schools', schools);
const port = process.env.PORT || 5001;

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});
